<?php
/**
 * 呼叫控制
 * 
 */
namespace Admin\Controller;
use Think\Controller;
use Common\Controller\BaseController;

class OmController extends BaseController {
		public function index(){
		$MEvent = D('Event');
		$wheres = array();
		$psize = I('get.psize', $this->m_psize,'intval');
		$verify = I('get.verify',false,'intval');
		$s_key = I('get.day','','trim');
		$this_month = date('Ym');
		$clerkname= I('get.clerkname','','trim');
   	 	$sector = I('get.sector',false,'intval');
   	 	$phonenum = I('get.phonenum','','trim');
   	 	$createtime = I('get.createtime');
   	 	$endtime = I('get.endtime');
   	 	$type = I('get.type');
		if ( $s_key!= undefined ) {
			if($s_key == "today")
	            $wheres['calllog.calldate'] = array('eq',date('Ymd'));
	        else if($s_key == "month")
	        	$wheres['calllog.calldate'] = array('like','%'.$this_month.'%');
	        else if("week" == $s_key )
	        	$wheres['calllog.calldate'] = array('between',$MEvent->getWeek());	
		}
		if( !$psize )
			$psize = $this->m_psize;
		else if( $psize > 20 )
			$psize = $this->m_psize;		
		if ($verify==0) {
			$calllog = D("calllog");
	        $childids =  $calllog->getchild();
			$wheres['_string'] = '1=1';
			$wheres['calllog.sector_id'] = array('in', $childids);
		}
		if ($verify == 1) {
			$calllog = D("calllog");
	        $childids =  $calllog->getchild();
			$wheres['calllog.sector_id'] = array('in', $childids);
			$getr = $calllog->getR();
			$wheres['_string'] = '1=1';
			$wheres['calllog.admin_no'] = $getr;            
		}
		if ($verify == 2){
			$calllog = D("calllog");
	        $childids =  $calllog->getchild();
			$wheres['calllog.sector_id'] = array('in', $childids);
	        $getroles =  $calllog->getrole();
			$wheres['_string'] = '1=1';
			$wheres['user.role_id'] = array('IN', $getroles);			
		}
        if ($sector && $sector != undefined)
        	 	$wheres['calllog.sector_id']=$sector;
        if ($phonenum && $phonenum != undefined)
        		$wheres['calllog.telephone']=$phonenum;
        if ($createtime !='' &&  $endtime!=''){
        		$wheres['calllog.begintime'] = array('EGT',$createtime);
        		$wheres['calllog.endtime'] = array('ELT',$endtime);	        	
        }    
        if ($clerkname && $clerkname !=undefined) {
        	    $user= M('user');
        	    $b_ = array();
        	    $b_['nickname']=$clerkname;
        	    $resu = $user->where($b_)->find();
        	    if ($resu) {
        	    	$wheres['calllog.admin_no']=$resu['user_id'];
        	    }else{
        	    	$wheres['calllog.admin_no']=0;
        	    }
       	}	
		if ($verify == 0 || $verify == 1)
			$cnt = $calllog->where( $wheres )->count();
		else{
			$cnt = $calllog	->join('user  ON calllog.admin_no = user.user_id')
							->where($wheres)
							->count();			
		}
		$Page = new \Common\ORG\Page( $cnt, $psize );
		$res = array('rcode'=>1,'data'=>NULL, 'msg'=> 'OK');
		$dlist = $calllog ->order('calllog.lasttime desc')
						  ->join('user ON calllog.admin_no = user.user_id')
						  ->where( $wheres )
						  ->limit($Page->firstRow.','.$Page->listRows )
						  ->select();
		$dlist = D('calllog')->trans($dlist);
		if( $dlist ){
			$res['rcode'] = 1;
			$res['data'] = $dlist;
			$res['pall'] =  $Page->getTotalPages();
			$res['p'] = $Page->nowPage;
			$res['psize'] = $psize;

		}else{
			$res['rcode'] = 2;
			$res['msg'] = "日志内容为空";
		}
		e_json($res);
	}
    //获得部门 
	public function getsector(){
		$arr = array();
		$calllog = D("calllog");
		$ad = $calllog->cs();//判断职位
		if ($ad[0]==0 || $ad[2]) {
        	$childids =  $calllog->getchild();
			$childids = array_splice($childids,1);
		}else{
			$childids =  $calllog->getchild();
		}
        $sector = M('sector');
        $arr['sector_id']=array('in',$childids);
        $res = $sector-> distinct(true)->field('sector_id,sector_name')->where($arr)->select();
        foreach ($res as $k => $v) {
        	if ($k==0) {
        		$res[$k]['sector_id']=0;
        		$res[$k]['sector_name']='全部';
        	}
        	$res[$k+1]['sector_id']=$v['sector_id'];
        	$res[$k+1]['sector_name']=$v['sector_name'];
        }
		echo json_encode( array("psector"=>array_values($res)));
		exit();			
	}
	// OM配置
	public function getOmlist(){
		$calOm = D("calllog");
		$OmServer = M('om_server');		
		$wheres = array();
		$psize = I('get.psize', $this->m_psize,'intval');	
		 // $r= $calOm->Cuid();
		/*
		搜索内容
		*/
			if (!$this->m_is_admin) {
				$User = M('user');
				$sector = M('sector');
				$arr = $att = array();
				$userId = $calOm->getR();
				$subcom = $sector ->join('user ON sector.sector_id = user.sector_id')
								  ->where('user.user_id = '.$userId)
								  ->find();
				$wheres['sub_company'] = $subcom['parent_company'];			
			}
			$wheres['_string'] = "1=1";
			if( !$psize )
				$psize = $this->m_psize;
			else if( $psize > 20 )
				$psize = $this->m_psize;

			$OmServer = M('om_server');
			$cnt = $OmServer->where($wheres)->count();
			$Page = new \Common\ORG\Page( $cnt, $psize );
			$res = array('rcode'=>1,'data'=>NULL, 'msg'=> 'OK');
			$dlist = $OmServer  ->order('omid desc')
							    ->where($wheres)
					            ->limit($Page->firstRow.','.$Page->listRows )
					            ->select();
			if( $dlist ){
				$res['rcode'] = 1;
				$res['data'] = $dlist;
				$res['pall'] =  $Page->getTotalPages();
				$res['p'] = $Page->nowPage;
				$res['psize'] = $psize;

			}else{
				$res['rcode'] = 2;
				$res['msg'] = "日志内容为空";
			}
		e_json($res);
	}

	// OM配置添加
	public function addOm(){
		$res = array('rcode'=>0,'data'=>NULL, 'msg'=> 'OK');		
		$calOm = D("calllog");
		$addname = I('post.add_name','','trim');
		$addaddress = I('post.add_address','','trim');
		$addIP = I('post.add_IPPBX','','trim');
		$port = I('post.port',false,'intval');
		$addrec = I('post.add_record','','trim');
		$addlist = I('post.add_list','','trim');
		$addads = I('post.add_Ads','','trim');
		$addport= I('post.add_port',false,'intval');
		$addcname= I('post.add_cname','','trim');
		$addpas= I('post.add_pass','','trim');
		$database= I('post.database','','trim');
		$rback = $calOm->add_om($addname,$addaddress,$addIP,$port,$addrec,$addlist,$addads,$addport,$addcname,$addpas,$database);
		if( $rback['r'] ){
			$res['rcode'] = 1;
			$res['msg'] = 'OK';
			$res['data'] = $rback['d'];
		}
		else{
			$res['msg'] = $addres['i'];
		}
		e_json( $res );
	}
   //OM配置编辑 
	public function editom(){
		$res = array('rcode'=>0,'data'=>NULL, 'msg'=> 'OK');
		$calOm = D("calllog");
		$id = I('post.omid',false,'intval');
		$addname1 = I('post.add_name1','','trim');
		$addaddress1 = I('post.add_address1','','trim');
		$addIP1 = I('post.add_IPPBX1','','trim');
		$port1 = I('post.port1',false,'intval');
		$addrec1 = I('post.add_record1','','trim');
		$addlist1 = I('post.add_list1','','trim');
		$addads1 = I('post.add_Ads1','','trim');
		$addport1= I('post.add_port1',false,'intval');
		$addcname1= I('post.add_cname1','','trim');
		$addpas1= I('post.add_pass1','','trim');
		$database1= I('post.database1','','trim');
		$rback = $calOm->EditOm($id,$addname1,$addaddress1,$addIP1,$port1,$addrec1,$addlist1,$addads1,$addport1,$addcname1,$addpas1,$database1);
		if( $rback['r'] ){
			$res['rcode'] = 1;
			$res['msg'] = 'OK';
			$res['data'] = $rback['data'];
		}
		else{
			$res['msg'] = $rback['m'];
		}
		e_json($res);
	}

   // 分机配置
	public function getOM(){
		$calOm = D('calllog');
		$rom = $calOm -> GOM();
		e_json($rom);
	}
  //分机配置 
	public function OMlist(){
		$calOmlist = D("calllog");
		$psize = I('get.psize', $this->m_psize,'intval');
		$wheres = array();

		$omtrans = I('get.type',0,'intval');
		$subphones = I('get.subphones','','trim');
		$paypersons = I('get.paypersons','','trim');
		$sectoradd = I('get.sectoradd',false,'intval');


		if ($subphones && $subphone!= undefined) {
			$wheres['om_subphone.subphone'] = $subphones;		
		}
		// 获取部门信息，如果有部门就是搜索部门的，没有就是部门及下属部门
		if (!$sectoradd || $sectoradd == false) {
	       $rbacks = $calOmlist ->getadmin();
	       $omsubphone = M('om_subphone');
	       $arr = array();
	       foreach ($rbacks as $k => $v) {
	         $arr[] = $v['user_id'];
	       }
       	   $wheres['om_subphone.admin_no'] = array("in",$arr);
		}else{
	       	$omsubphone = M('om_subphone');
       		$user = M('user');
       		$arr_ = array();
       		$arr = array();
       		$arr_['sector_id'] = $sectoradd;
       		$res_ = $user->where($arr_)->select();
       		foreach ($res_ as $k => $v) {
       			 $arr[] = $v['user_id'];
       		}
       	   $wheres['om_subphone.admin_no'] = array("in",$arr);
       }
		if ($paypersons && $paypersons != undefined) {
		   $userTrans  = M('user');
	       $omsubphone = M('om_subphone');		   
	       $Rb = $calOmlist ->getadmin();
	       $azz = $att = array();
	       foreach ($Rb as $k => $v) {
	         $azz[] = $v['user_id'];
	       }
			$att['nickname'] = $paypersons;
			$ruser = $userTrans->where($att)->find();
			$ub=$ruser['user_id'];
			if ($ub && in_array($ub, $azz)) {
				$wheres['om_subphone.admin_no'] = $ub;
			}
		}
		$wheres['_string'] = "1=1";
		if( !$psize )
			$psize = $this->m_psize;
		else if( $psize > 20 )
			$psize = $this->m_psize;

       	if ($omtrans !=0) {
          $wheres['om_subphone.omid'] = $omtrans;
       	}

		$cnt = $omsubphone->where($wheres)->count();
		$Page = new \Common\ORG\Page( $cnt, $psize );
		$res = array('rcode'=>1,'data'=>NULL, 'msg'=> 'OK');
	    $dlist = $omsubphone 
	                        ->field('om_server.name,om_subphone.subphone,om_subphone.line_id,user.nickname,om_server.omid,om_subphone.id')
	                        ->join('om_server ON om_subphone.omid = om_server.omid')
	                        ->join('user ON user.user_id = om_subphone.admin_no')
	                        ->where($wheres)
	                        ->limit($Page->firstRow.','.$Page->listRows )
	                        ->select();	
		if( $dlist ){
			$res['rcode'] = 1;
			$res['data'] = $dlist;
			$res['pall'] =  $Page->getTotalPages();
			$res['p'] = $Page->nowPage;
			$res['psize'] = $psize;
		}else{
			$res['rcode'] = 2;
			$res['msg'] = "日志内容为空";
		}
		e_json($res);
	}
  //添加分机配置 
   public function addOmconfig(){
	  $res = array('rcode'=>'','data'=>NULL, 'msg'=> 'OK');   	
   	  $bindperson = I('get.bindperson','','trim');
   	  $subtelphone = I('get.subtelphone','','trim');
   	  $add_device = I('get.add_device',false,'intval');
   	  $calcon = D('calllog');
   	  $rback = $calcon ->addcon($bindperson,$subtelphone,$add_device);
		if( $rback['r'] ){
			$res['rcode'] = 1;
			$res['msg'] = 'OK';
			$res['data'] = $rback['d'];
		}
		else{
			$res['msg'] = $rback['i'];
		}
   	  e_json($res);
   }
   public function getdevice(){
   	 $col = D('calllog');
   	 $res = $col->getdev();
   	 e_json($res);
   }
   // 编辑分机配置
   public function editphone(){
	  $res = array('rcode'=>'','data'=>NULL, 'msg'=> 'OK'); 
	  $id = I('post.subid',false,'intval');  	
   	  $bindperson1 = I('post.bindperson1','','trim');
   	  $subtelphone1 = I('post.subtelphone1','','trim');
   	  $add_device1 = I('post.add_device1',false,'intval');
   	  $calcon = D('calllog');
   	  $rback = $calcon ->editcon($id,$bindperson1,$subtelphone1,$add_device1);
		if( $rback['r'] ){
			$res['rcode'] = 1;
			$res['msg'] = 'OK';
			$res['data'] = $rback['d'];
		}
		else{
			$res['msg'] = $rback['i'];
		}
	e_json($res);
   }	
   // 获得员工人数
   public function getclerk(){
   	$calllog = D('calllog');
   	$user = M('user');
   	$arr = array();
   	$resector = $calllog->getchild();
   	$arr['sector_id'] = array('IN',$resector);
   	$res = $user ->field('nickname')->where($arr)->select();
   	e_json($res);
   }
  //时长统计 
   public function getcalmsg(){
   	 $calmsg = D('calllog');
   	 $Caluser = M('user');
   	 $sectorF = M('sector');
   	 $att = $dlist = $amm = array();
   	 $extnum = I('get.ext_id','','trim');
   	 $creatime = I('get.starttime');
   	 $endtime = I('get.endtime');
   	 $style = I('get.sty',1,'intval');
   	 $sector = I('get.sector');
   	 if ($style == 2) {//加载部门的时候
   	 	$rep1 = $calmsg->cs();
   	 	if (!$rep1[2]) {
		   	 if ($sector == '' || $sector == 0) {//刚开始加载自己所属部门，从后台直接加载
			    $res1 = $calmsg->cs();
			    $sector = $res1[0];
			    $dlist['sector_id'] = $res1[1];//判断职位
		   	 }
   	 	}else{
		   	 if ($sector == '') {//刚开始加载自己所属部门，从后台直接加载
			    $res1 = $calmsg->cs();
			    $sector = $res1[0];
			    $dlist['sector_id'] = 2;//判断职位,返回到前端对搜索框部门进行显示
		   	 }   	 		
   	 	}
   	 }
   	 if ($style ==1) {//加载个人的时候
	   	 if ($sector == '' || $sector == 0) {
		    $dlist['sector_id'] = 0;
	   	 }
   	 }
   	 // 做区分是点击部门按钮还是点击个人按钮
   	 if ($style == 1) {
   	 	$calres = $calmsg ->getchild();
   	 }else{
   	 	$calres = $calmsg ->get_child();	
   	 }
   	 // 
   	 if ($extnum && $extnum != undefined) {
		$att['calllog.ext_id'] = $extnum;
   	 }
   	 if ($creatime && $endtime) {
   	 	$att['calllog.begintime'] = array('between',array($creatime,$endtime));
   	 	$amm['calllog.begintime'] = array('between',array($creatime,$endtime));
   	 }
   	 if ($style == 1) {//当点击显示员工的按钮的时候 
   	 	$arr = $a = $b = $c = array();  	 	
   	 	$arr['sector.sector_id'] = array('IN',$calres); 
   	 	if (!$sector) {//判断部门是否为为空
	   	 $res =  $Caluser->field('user.user_id,user.nickname,user.sector_id')
	   	 				 ->join('sector ON user.sector_id = sector.sector_id')
	   	 				 ->where($arr)
	   	 				 ->select();
   	 	}else{
   	 	 $m = array();
   	 	 $cc = $calmsg->getchild1($sector);//将所选择的部门的id取出来
   	 	 $m['sector.sector_id']=array('in',$cc);
   	 	 // 将部门及下属部门的员工取出来
	   	 $res =  $Caluser->field('user.user_id,user.nickname,user.sector_id')
	   	 				 ->join('sector ON user.sector_id = sector.sector_id')
	   	 				 ->where($m)
	   	 				 ->select();  	 		
   	 	}  	 	
	   	 foreach ($res as $k => $v) {
	   	 	 $att['calllog.admin_no'] = $v['user_id'];
	   	 	 $rbk = $calmsg ->where($att)->select();	   	 	 	
		   	 	if ($rbk) { //说明有通话记录。打过电话
		   	 		$a[]=$calmsg->where($att)->sum(calltime);
		   	 		$b[]=$v['nickname'];
		   	 	}else{
		   	 		$a[]=0;
		   	 		$b[]=$v['nickname'];
		   	 	}
	   	 }
	   	$c=array_combine($b,$a);
	   	asort($c,SORT_NUMERIC);
	   	$c[] = array_reverse( $c );
	   	foreach ($c as $k_ => $v_) {
   			foreach ($v_ as $k1_ => $v1_) {
	   			$dlist['nickname'][]=$k1_;
	   			$dlist['timelong'][]=intval($v1_/60);
   			}
	   	}	   	 
   	 }else{
   	 	if ($calres[0]=='m') {//如果是总监及部门经理
	   	 	$arr = $a = $b = $c = array(); 
	   	 	$vf = $calmsg->cs();
	   	 	if ($vf[0]==0) {//如果是总监
	   	 		$calres = array_splice($calres,2);	
	   	 	}else{
	   	 		$calres = array_splice($calres,1);	 
	   	 	}	
	   	 	$arr['sector_id'] = array('IN',$calres); 
	   	 	if (!$sector) {
		   	 $res =  $sectorF->field('sector_name,sector_id,parent_sector')
		   	 				 ->where($arr)
		   	 				 ->select();
		   	 $dlist['sector_id'] = 1; 
	   	 	}else{
		   	 $res =  $sectorF->field('sector_name,sector_id,parent_sector')
		   	 				 ->where('sector_id='.$sector)
		   	 				 ->select();   	 		
	   	 	} 	
		   	 foreach ($res as $k => $v) {
		   	 	 $att['calllog.sector_id'] = $v['sector_id'];	   	 	 	
		   	 	 $rbk = $calmsg ->where($att)->select();	   	 	 	
			   	 	if ($rbk) { //说明有通话记录。打过电话
			   	 		$a[]=$calmsg->where($att)->sum(calltime);
			   	 		$b[]=$v['sector_name'];	
			   	 	}else{
			   	 		$a[]=0;
			   	 		$b[]=$v['sector_name'];
			   	 	}
		   	 }   	 		
   	 	}else{//如果是总监以上的职位登录的话
		   	 	$arr = $a = $b = $c = $e = $att_= $as = array(); 
		   	 	if ($sector == '' || $sector == 0) {//如果搜索框没有选择
			   	 	// 获取战区的id
		   	 		foreach ($calres['state'] as $k => $v) {
		   	 			if ($v == 'a') {
		   	 				$as[]=$calres['sector_id'][$k];
		   	 			}	 		
	   	 			}
	   	 			$e['sector_id']= array('IN',$as);
	   	 			$res = $sectorF->where($e)->select();
	   	 			foreach ($res as $k_ => $v_) {
	   	 				$b[]=$v_['sector_name'];
	   	 			}
	   	 		// 获取战区的时长
	   	 			for ($i=0; $i < count($as) ; $i++) { 
	   	 				unset($att_);
	   	 				foreach ($calres['state'] as $k1 => $v1) {
	   	 					if ($v1==$as[$i]) {
	   	 						$att_[]=$calres['sector_id'][$k1];
	   	 					}
	   	 				}
	   	 				$att_[]=$as[$i];//将战区自身的id也放进去
	   	 				$amm['sector_id'] = array('in',$att_);
	   	 				$a[]=$calmsg->where($amm)->sum(calltime);    				
	   	 			}
	   	 			$dlist['sector_id']=1;
		   	 	}else{//如果搜索框有选择
		   	 			$k = $sectorF->where('sector_id ='.$sector)->find();
		   	 			$b[]=$k['sector_name'];
		   	 			// 获取战区的时长
	   	 				$aa = $calmsg->getchild1($sector);
	   	 				$amm['calllog.sector_id'] = array('in',$aa);
	   	 				$a[]=$calmsg->where($amm)->sum(calltime);    						   				 	 		
		   	 	}  	 	 
			}
		   	$c=array_combine($b,$a);
		   	asort($c,SORT_NUMERIC);
		   	$c[] = array_reverse( $c );
		   	foreach ($c as $k_ => $v_) {
	   			foreach ($v_ as $k1_ => $v1_) {
		   			$dlist['sector_name'][]=$k1_;
		   			$dlist['timelong'][]=intval($v1_/60);
	   			}
		   	} 
   		}
   	e_json($dlist);
   }
}